package com.hlingsoft.cms.action;

import com.hlingsoft.cms.alipay.util.httpClient.HttpRequest;
import com.hlingsoft.cms.dao.ConsigneeDao;

import com.hlingsoft.cms.entity.Consignee;
import com.hlingsoft.cms.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/5/6.
 */

@Controller
@RequestMapping("/consignee")
public class ConsigneeAction extends BaseAction {

    @Autowired
    private ConsigneeDao consigneeDao;


    /**
     * 添加收货人
     * @param appUserId
     * @param name
     * @param phone
     * @param address
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "appUserId") long appUserId,
            @RequestParam (value = "name",required = false) String name,
            @RequestParam (value = "phone",required = false) String phone,
            @RequestParam (value = "area",required = false)  String area,
            @RequestParam (value = "address",required = false) String address,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Consignee consignee = new Consignee();
            Date now = new Date();
            if (request.getMethod().equals(HttpRequest.METHOD_GET)){
                name=Common.encodeStr(name);
                area=Common.encodeStr(area);
                address=Common.encodeStr(address);
            }
            consignee.setName(name);
            consignee.setPhone(phone);
            consignee.setArea(area);
            consignee.setAddress(address);
            consignee.setAppUserId(appUserId);
            consignee.setCreateTime(now);
            consignee.setUpdateTime(now);

            //第一次添加收件人设值为默认收件人
            if (consigneeDao.getConsigneeByAppUserId(appUserId).size()==0){
                consignee.setIsDefault(1);
            }else {
                consignee.setIsDefault(0);
            }
            consigneeDao.addConsignee(consignee);
            resultMap.put("consignee", consignee);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }



    /**
     * 编辑收货人,设置默认简历
     * @param consigneeId
     * @param name
     * @param phone
     * @param address
     * @param isDefault
     * @param request
     * @return
     */
    @RequestMapping(value = "/edit.json")
    @ResponseBody
    public Map<String, Object> edit(
            @RequestParam(value = "consigneeId") long consigneeId,
            @RequestParam (value = "name",required = false) String name,
            @RequestParam (value = "phone",required = false) String phone,
            @RequestParam(value = "area",required = false) String area,
            @RequestParam (value = "address",required = false) String address,
            @RequestParam(value = "isDefault",required = false) Integer isDefault,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            Consignee consignee = consigneeDao.getConsigneeById(consigneeId);
            if (consignee!=null){

                if (request.getMethod().equals(HttpRequest.METHOD_GET)){
                    name=Common.encodeStr(name);
                    area=Common.encodeStr(area);
                    address=Common.encodeStr(address);
                }

                if (name!=null){
                    consignee.setName(name);
                }
                if (phone!=null){
                    consignee.setPhone(phone);
                }
                if (area!=null){
                    consignee.setArea(area);
                }
                if (address!=null){
                    consignee.setAddress(address);
                }
                if (isDefault!=null){
                    //设置默认简历
                    if (isDefault.intValue()==1){
                        Consignee defaultConsignee = consigneeDao.getDefaultConsigneeByAppUserId(consignee.getAppUserId());
                        //存在默认简历,更新之前的默认简历状态修改
                        if (defaultConsignee!=null){
                            defaultConsignee.setIsDefault(0);
                            consigneeDao.updateConsignee(defaultConsignee);
                        }
                    }
                    consignee.setIsDefault(isDefault.intValue());
                }
                consignee.setUpdateTime(new Date());

                consigneeDao.updateConsignee(consignee);
            }  else {
                resultMap.put("msg","简历不存在");
                resultMap.put("result",false);
            }
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 用户收货人列表
     * @param appUserId
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(value = "appUserId") long appUserId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Consignee> consigneeList = consigneeDao.getConsigneeByAppUserId(appUserId);
            resultMap.put("consigneeList",consigneeList);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 删除收货人
     * @param consigneeId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam(value = "consigneeId") long consigneeId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            consigneeDao.deleteConsigneeById(consigneeId);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

}
